package com.uchengwang.ui.cm_ui.uu.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseNoTitleActivity;
import com.uchengwang.ui.cm_ui.uu.adapter.XiangliaoRelationAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.ChatNoticeBean;
import com.uchengwang.ui.cm_ui.uu.bean.XiangliaoBean;
import com.uchengwang.ui.cm_ui.uu.http.XiangliaoManager;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2018/4/17.
 */

public class XiangliaoRelationActivity extends BaseNoTitleActivity{
    private ImageView relation_head, relation_type, relation_left, relation_gif;
    private TextView relation_name;
    private PtrClassicFrameLayout relation_refresh;
    private RecyclerView relation_rv;

    private XiangliaoRelationAdapter relationAdapter;
    private RecyclerAdapterWithHF mAdapter;

    private int page =1, totlaPagers;
    private XiangliaoBean xiangliaoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_xiangliao_relation);
//        StatusBarUtil.getUtil().setTransparencyBar(this);

        initView();
    }

    private void initView() {
        relation_gif = (ImageView) findViewById(R.id.relation_gif);
        relation_head = (ImageView) findViewById(R.id.relation_head);
        relation_type = (ImageView) findViewById(R.id.relation_type);
        relation_name = (TextView) findViewById(R.id.relation_name);
        relation_refresh = (PtrClassicFrameLayout) findViewById(R.id.relation_refresh);
        relation_rv = (RecyclerView) findViewById(R.id.relation_rv);
        relation_left = (ImageView) findViewById(R.id.relation_left);
        relation_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XiangliaoRelationActivity.this.finish();
            }
        });

        initData();

    }

    private void initData(){
        ImageManager.getInstance().setRoundSize(0).loadImage(this, relation_gif, R.drawable.image_want_chat_bottom, ImageManager.ImageType.ROUND);


        ImageManager.getInstance().loadImage(this, relation_head, UChengApplication.userAvatar, ImageManager.ImageType.CIRCLE);
        relation_name.setText(UChengApplication.nickName);


        List<XiangliaoBean> beanList = LiteOrmDBUtil.getLiteOrm(this).getQueryByWhere(XiangliaoBean.class, "uid", String.valueOf(UCommonUtil.getUtil().getId()));
        if (beanList != null && beanList.size() >0){
            xiangliaoBean = beanList.get(0);
        }

        if (xiangliaoBean != null){
            int xiangLiaoType = xiangliaoBean.getEmotionType();
            if (xiangLiaoType > 0){
                ImageManager.getInstance().loadImage(this, relation_type, XiangliaoManager.getManager().getEmotionResId(xiangLiaoType), ImageManager.ImageType.NARMOL);
            }

        }

//        int xiangLiaoType = SPUtils.getPreferences(this).getXiangLiaoType();
//        ImageManager.getInstance().loadImage(this, relation_type, XiangliaoManager.getManager().getEmotionResId(xiangLiaoType), ImageManager.ImageType.NARMOL);



        relation_rv.setLayoutManager(new LinearLayoutManager(this));
        relationAdapter = new XiangliaoRelationAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(relationAdapter);

        relation_rv.setAdapter(mAdapter);
//
        relation_refresh.setAutoLoadMoreEnable(true);
        relation_refresh.setLoadMoreEnable(true);


        relation_refresh.postDelayed(new Runnable() {
            @Override
            public void run() {
                relation_refresh.autoRefresh(true);
            }
        }, 150);

        relation_refresh.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
               page =1;
               getData(true);

            }
        });

        relation_refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getData(false);
                } else {
                    relation_refresh.loadMoreComplete(false);
                }
            }
        });
    }

    private void getData(final boolean isRefresh){
        MapRequest map= new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 20);
        map.put(BaseConfig.UPDATE_READ, true);
        map.put(BaseConfig.NOTICE_TYPE, 2);
        XiangliaoManager.getManager().getChatNotices(this, map, new OnHttpResultListener<ChatNoticeBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<ChatNoticeBean> list) {
                totlaPagers = total;
                if (isRefresh){
                    relation_refresh.refreshComplete();
                }else {
                    relation_refresh.loadMoreComplete(true);
                }
                if (list != null){
                    if (isRefresh){
                        relationAdapter.setData(list);
                    }else {
                        relationAdapter.getData().addAll(list);
                        relationAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

    }
}
